/*
 ============================================================================
 Name        : consoleParser.c
 Author      :
 Version     :	1
 Copyright   : Your copyright notice
 Description : Parse console input
 ============================================================================
 */

#include "consoleParser.h"
#include <debug.h>


char inputCommandArgs[MAX_INPUT_ARGS];///<Array containing incoming commands
char inputArgList[MAX_INPUT_ARGS];///<Array containing the list of arguments for the application execution

unsigned char numberInputCommands = 0; ///<Number of input commands
unsigned char numberInputArgs = 0; ///< Number of input arguments

typedef struct
{
/// Structure containing the different program process
	char inCharacter; ///<Process Name
	char *helpString;///<Process description
	unsigned char used;///<Boolean that specifies the procedure is used or not
} tsConsoleInput; ///< Structure containing the different program process

tsConsoleInput inputCommands[] =
{
{ HEADER_DUMP, "Prints the headers name of the file\n", 0 },
{ RAW_DUMP, "Hex dump of the file\n", 0 },
{ PROCESS_HEADER_CONTENT, "Prints the content of each header\n", 0 },
{ HELP, "Prints this help\n", 0 },
{ 0, 0, 0 } };
/*! \fn void printHelp()
\brief Prints menu options.
*/
char printHelp(int fd)
{
	printf("----------------------------------------\n");
	printf("                HELP\n");
	printf("----------------------------------------\n");
	tsConsoleInput *pInput = inputCommands;
	while (pInput->inCharacter != 0)
	{
		printf("-%c \t%s", pInput->inCharacter, pInput->helpString);
		pInput++;
	}
	printf("----------------------------------------\n");
	return STATUS_SUCCESS;
}
/*! \fn char char validateInput(char * pArg)
\brief Validates the input arguments, to verify whether they correspond to a valid program option.
\param pArg Argument to validate.
*/
char validateInput(char * pArg)
{

	//printf("%d\n",numberInputArgs);
	if (numberInputArgs > 1)
	{
		printf("Just one file can be procesed.\n");
		//printf("%s\n",pArg);
		return STATUS_ERROR;
	}

	while (*pArg)
	{
		char found = 0;
		tsConsoleInput *pInput = inputCommands;
		while (pInput->inCharacter != 0)
		{
			if (*pArg == pInput->inCharacter)
			{
				found = 1;
				break;
			}
			pInput++;
		}
		if (!found)
		{
			return STATUS_ERROR;
		}
		pArg++;
	}

	return STATUS_SUCCESS;
}
/*! \fn processArgs(int argc, char* argv[], char** pArgFiles, char * pArg)
\brief A process that divides the entry list into commands and arguments to be used in the program execution.
\param argc Number of arguments.
\param arg[] Incoming argument array
\param pArgFiles Name File argument
\param pArg Input commands
*/

char processArgs(int argc, char* argv[], char** pArgFiles, char * pArg)
{
	char** pIndex = argv;
	pIndex++; //Skip file name

	if (argc > MAX_INPUT_ARGS)
	{
		//printf("Maximun arg limit its: %d", MAX_INPUT_ARGS);
		return STATUS_ERROR;
	}

	while (argc > 1)
	{
		if (pIndex[0][0] == '-') //check for commands
		{
			*pArg = pIndex[0][1];
			PRINT_DEBUG(DEBUG_ALL, "Adding: %c to command list...\n", *pArg);
			//printf("pArg:%s\n",pArg);
			numberInputCommands++;
			pArg++;
		}
		else
		{
			pArgFiles[0] = pIndex[0];
			PRINT_DEBUG(DEBUG_ALL,
						"Adding: %s to args list...\n",
						pArgFiles[0]);
			//printf("pArgFile%s\n",pArgFiles[0]);
			numberInputArgs++;
			pArgFiles++;
		}
		argc--;
		pIndex++;
	}
	return STATUS_SUCCESS;
}
/*! \fn parseConsoleInput(int argc, char* argv[], char** pArgFiles, char * pArg)
\brief Parse the arguments entered as input to identify the file name and the commands to be executed. Validating ever input command.
\param argc Number of arguments.
\param arg[] Array of arguments.
\param pArgFiles Name File argument
\param pArg Commands
*/

char parseConsoleInput(int argc, char* argv[], char** pArgFiles, char * pArg)
{
	unsigned int status = STATUS_SUCCESS;
	numberInputCommands = 0;
	numberInputArgs = 0;

	status = processArgs(argc, argv, pArgFiles, pArg);
	if (ERROR(status))
	{
		return STATUS_ERROR;
	}

	status = validateInput(pArg);
	if (status == STATUS_ERROR)
	{
		printHelp(1);
		return STATUS_ERROR;
	}
	PRINT_DEBUG(DEBUG_ALL, "Num commands: %x\n", numberInputCommands);
	PRINT_DEBUG(DEBUG_ALL, "Num Args: %x\n", numberInputArgs);
	return status;
}
